import Vue from 'vue';
import * as Fiber from 'fiber';

const filterButtons = function (needButtonList, allButtons) {
  let result = [];
  needButtonList.forEach(item => {
    const hasSubMenu = Array.isArray(item);
    const key = hasSubMenu ? item[0] : item;
    const hasAuth = allButtons[key].hasAuth;
    if (hasAuth || hasSubMenu) {
      let buttonConfig = {
        id: key,
        name: allButtons[key].name,
        type: allButtons[key].type
      };
      if (hasSubMenu) {
        const subMenuList = filterButtons(item[1], allButtons);
        if (subMenuList.length > 0) {
          buttonConfig.subMenu = subMenuList;
          result.push(buttonConfig);
        }
      } else {
        result.push(buttonConfig);
      }
    }
  });
  return result;
};
const isPigeonholeBtn = function () {
  const hasResourceCode =
    window._hasResource('F04_docIndex') ||
    window._hasResource('F04_myDocLibIndex') ||
    window._hasResource('F04_accDocLibIndex') ||
    window._hasResource('F04_proDocLibIndex') ||
    window._hasResource('F04_eDocLibIndex') ||
    window._hasResource('F04_docLibsConfig');
  return hasResourceCode;
};
// 根据权限以及业务需求返回操作按钮
export const initToolButtonList = function (needButtonList, { canDel, showHandoverButton, hasAIPlugin, hasDoc, showStepbackRecord, isVJMember, isV5Member, hasDumpData }) {
  const allButtons = {
    transmit: {
      name: Fiber.i18n('common.toolbar.transmit.label'),//转发
      type: 'default',
      hasAuth: false,
    },
    transmitCol: {
      name: Fiber.i18n('common.toolbar.transmit.col.label'),//转发协同
      type: 'default',
      hasAuth: false,
    },
    transmitMail: {
      name: Fiber.i18n('common.toolbar.transmit.mail.label'),//转发邮件
      type: 'default',
      hasAuth: false,
    },
    pigeonhole: {
      name: Fiber.i18n('common.toolbar.pigeonhole.label'),//归档
      type: 'default',
      hasAuth: false,
    },
    delete: {
      name: Fiber.i18n('common.toolbar.delete.label'),//删除
      type: 'default',
      hasAuth: false,
    },
    takeBack: {
      name: Fiber.i18n('common.toolbar.takeBack.label'),//取回
      type: 'default',
      hasAuth: true,
    },
    stepbackRecord: {
      name: Fiber.i18n('collaboration.workflow.label.repealStepback'),//撤销回退记录
      type: 'default',
      hasAuth: false,
    },
    handover: {
      name: Fiber.i18n("collaboration.other.label"),//他人事项
      type: 'default',
      hasAuth: false,
    },
    handoverToMe: {
      name: Fiber.i18n("handover.button.to.js"),//交接给我的事项
      type: 'default',
      hasAuth: false,
    },
    handoverFromMe: {
      name: Fiber.i18n("handover.button.from.js"),//交接给他人的事项
      type: 'default',
      hasAuth: false,
    },
    otherGrab: {
      name: Fiber.i18n("collaboration.other.grab"),//他人竞争处理事项
      type: 'default',
      hasAuth: false,
    },
    aiProcessing: {
      name: Fiber.i18n('collaboration.portal.listDone.aiProcessingRecord'),//只列出智能处理
      type: 'checkbox',
      hasAuth: false,
    },
    deduplication: {
      name: Fiber.i18n('collaboration.portal.listDone.isDeduplication'),//同一流程只显示一条
      type: 'checkbox',
      hasAuth: true,
    },
    smartCodeScan: {
      name: Fiber.i18n('common.barcode.search.saoyisao'),
      type: 'default',
      hasAuth: true,
    },
    currentData: {
      name: Fiber.i18n('collaboration.portal.listDone.currentData.js'),
      type: 'default',
      hasAuth: false,
    },
    dumpData: {
      name: Fiber.i18n('collaboration.portal.listDone.dumpData.js'),
      type: 'default',
      hasAuth: false,
    },
    //已办事项 新增“知会”按钮
    inform:{
      name: Fiber.i18n('collaboration.nodePerm.addInform.label'),
      type: 'default',
      hasAuth: true,
    }
  };
  //转发协同
  if (window._hasResource('F01_newColl')) {
    allButtons.transmitCol.hasAuth = true;
    // click: transmitCol
  }
  //转发邮件
  if (window._hasResource('F12_mailcreate')) {
    const emailShow = window._hasPlugin('webmail');
    if (emailShow) {
      allButtons.transmitMail.hasAuth = true;
      // click: transmitMail
    }
  }
  //归档
  if (isPigeonholeBtn() && hasDoc == true) {
    allButtons.pigeonhole.hasAuth = true;
  }
  //删除
  if (canDel === true) {
    allButtons.delete.hasAuth = true;
  }
  //撤销回退记录
  if (showStepbackRecord == true) {
    allButtons.stepbackRecord.hasAuth = true;
  }
  //他人事项
  if (showHandoverButton == true && isV5Member) {
    allButtons.handoverToMe.hasAuth = true;
    allButtons.handoverFromMe.hasAuth = true;
    allButtons.otherGrab.hasAuth = true;
    allButtons.handover.hasAuth = true;
  }
  //他人事项，VJOIN屏蔽交接相关的2个，保留竞争的一个
  if (isVJMember) {
    allButtons.otherGrab.hasAuth = true;
    allButtons.handover.hasAuth = true;
  }
  //"只列出智能处理"
  if (hasAIPlugin == true) {
    allButtons.aiProcessing.hasAuth = true;
  }

  if (hasDumpData == "true") {
    allButtons.currentData.hasAuth = true;
    allButtons.dumpData.hasAuth = true;
    needButtonList.push('currentData');
    needButtonList.push('dumpData');
  } //toolbar扩展
  let result = filterButtons(needButtonList, allButtons);
  // result[0].type = 'primary';
  return result;
};
